package com.itheima.health.utils;

import com.itheima.health.pojo.OrderSetting;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 山外小王
 * @Description
 * @Date 2024/04/17/16:56
 */
@Component
public class POIUtils {

    /**
     * 读取文件
     * @return
     */
    public List<OrderSetting> readExcel(MultipartFile file) throws IOException {
        //获取上传文件的输入流
        InputStream inputStream = file.getInputStream();
        //创建返回对象
        List<OrderSetting> orderSettingList = new ArrayList<>();
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

        //获取第一个sheet工作表表
        XSSFSheet sheet = workbook.getSheetAt(0);
        int index = 1;
        while(sheet.getRow(index)!=null){
            Date dateCellValue = sheet.getRow(index).getCell(0).getDateCellValue();
            int numberCellValue = (int) sheet.getRow(index).getCell(1).getNumericCellValue();
            OrderSetting orderSetting = new OrderSetting(dateCellValue,numberCellValue);
            orderSettingList.add(orderSetting);
            index++;
        }
        return orderSettingList;
    }
}
